﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BUS;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace GUI
{
    public partial class ucBase<T,D> : UserControl
        where T : Entity, new()
        where D : ManamentBase<T>, new()
    {
        protected SimpleButton btnThem = null;
        protected SimpleButton btnXoa = null;
        protected SimpleButton btnSua = null;
        protected SimpleButton btnClear = null;
        protected GridControl _grid = null;
        protected GridView _view = null;
        protected BindingSource _bind = null;
        public ucBase()
        {
        }
        public virtual void Init()
        {
            _bind = new BindingSource();
            _bind.AllowNew = true;
            _view = ((GridView)_grid.MainView);
            GetData();
            btnThem.Click += new EventHandler(btnThem_Click);
            btnXoa.Click += new EventHandler(btnXoa_Click);
            btnSua.Click += new EventHandler(btnSua_Click);
            btnClear.Click += new EventHandler(btnClear_Click);
            _view.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(_view_SelectionChanged);
        }

        void btnClear_Click(object sender, EventArgs e)
        {
            T obj = new T();
            SetData(obj);
        }

        void _view_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            int[] i = ((GridView)_grid.MainView).GetSelectedRows();
            if (i.Length > 0)
                SetData((T)((GridView)_grid.MainView).GetRow(i[0]));
        }
        public void GetData()
        {
            D _BUS = new D();
            _bind.DataSource = _BUS.GetData();
            _grid.DataSource = _bind;
        }

        protected virtual void btnSua_Click(object sender, EventArgs e)
        {
            D _BUS = new D();
            int[] i = ((GridView)_grid.MainView).GetSelectedRows();
            if (i.Length > 0)
            {
                T obj = (T)((GridView)_grid.MainView).GetRow(i[0]);
                GetData(ref obj);
                if (_BUS.Update((Entity)obj))
                {
                    _bind[i[0]] = obj;
                   // MessageBox.Show("Sửa Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Sửa Thất Bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        protected virtual void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa thông tin này không ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                D _BUS = new D();
                int[] n = ((GridView)_grid.MainView).GetSelectedRows();
                for (int i = n.Length - 1; i >= 0; --i)
                {
                    if (_BUS.Delete((Entity)((GridView)_grid.MainView).GetRow(n[i])))
                    {
                        _bind.RemoveAt(n[i]);
                    }
                }
                //MessageBox.Show("Xóa Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected virtual void btnThem_Click(object sender, EventArgs e)
        {
            D _BUS = new D();
            T obj = new T();
            GetData(ref obj);
            if (_BUS.Insert((Entity)obj))
            {
                _bind.Add(obj);
                //MessageBox.Show("Thêm Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Thêm Thất Bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public virtual void SetData(T obj)
        {
            throw new NotImplementedException();
        }
        public virtual void GetData(ref T obj)
        {
            throw new NotImplementedException();
        }
    }
}
